#!/bin/bash
# 操作系统相关的函数库(centos)
# author    : guyadong
# date      : 2019/08/21
# 导入标志,确保只会被导入一次
native_functions_included=true
# 脚本初始化
[ "$common_init_included" != 'true' ] && . ./common_init.sh

function need_install_list {
    local list=
    need_install_mysql && list="$list mysql-server"
    need_install_redis && list="$list redis"
    need_install_nginx && list="$list nginx"
    need_install_java8 && list="$list java-1.8.0-openjdk"
    need_install_node  && list="$list nodejs"
    echo $list
}
function need_install_build_tools {
    local list=
    jdk8_available || list="$list java-1.8.0-openjdk-devel"
    node_available || list="$list nodejs"
    git_available  || list="$list git"
    echo $list
}

# 安装 nginx.repo
# centos7已经自带了nginx,安装 nginx.repo的目的是避免安装centos7自带的1.13版本，
# 因为该版本的 nginx.conf 不好自动化处理
function prepare_nginx(){
    local nginx_repo=/etc/yum.repos.d/nginx.repo
    if [ ! -f $nginx_repo ]
    then
        echo "安装 $nginx_repo"
        # 定义版本ID(VERSION_ID)
        eval $(cat /etc/os-release | grep VERSION_ID)
        VERSION_ID=${VERSION_ID%%.*}
        local content="[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/$VERSION_ID/\$basearch/
gpgcheck=0
enabled=1"
        sudo sh -c "echo '$content'>$nginx_repo " || exit_on_error
    fi

}
# 安装 mysql-community.repo
function install_mysql_community_release_if_needed(){
    local name='mysql57-community-release-el7-8.noarch'
    if ! rpm -qa|grep -F $name >/dev/null
    then
        pushd /tmp >/dev/null
        rm -f $name.rpm || exit_on_error
        # wget http://repo.mysql.com/$name.rpm || exit_on_error
        wget http://dev.mysql.com/get/$name.rpm || exit_on_error
        sudo rpm -ivh $name.rpm || exit_on_error
        rm -f $name.rpm
        popd >/dev/null
    fi 
}
function prepare_node(){
        yum repolist | grep nodesource 1>/dev/null 2>&1 && return 0
        #rpm -qa 'nodejs' |grep nodejs-10 && return 0
        local cmd='curl -sL https://rpm.nodesource.com/setup_10.x | bash -  '
        if which getenforce 1>/dev/null 2>&1&& [  "$(whoami)"  != 'root ' ]
        then
            echo "下面这条命令需要root 用户执行，请输入root用户密码"         
            su root sh -c "$cmd" || exit_on_error
        else
            sudo sh -c "$cmd"
        fi
}

function prepare_redis(){
    if ! yum repolist| grep epel >/dev/null 2>&1
    then
        # 添加 fedora 源
        sudo yum -y install epel-release || exit_on_error
    fi
}
# 安装指定的软件包
# $1 待安装的软件包列表
function install_components {
    [ -z "$1" ] && return 0    
    [[ "$1" == *mysql* ]] && install_mysql_community_release_if_needed
    [[ "$1" == *redis* ]]  && prepare_redis
    [[ "$1" == *nginx* ]]  && prepare_nginx
    [[ "$1" == *nodejs* ]]  && prepare_node
    sudo yum updateinfo || exit_on_error
    sudo yum -y install $1 || exit_on_error

    if [[ "$1" == *mysql* ]]
    then 
        sudo systemctl start mysqld || exit_on_error
    fi
    if [[ "$1" == *redis* ]]
    then 
        sudo systemctl start redis || exit_on_error
    fi
}
# 卸载指定的软件包
# $1 待卸载的软件包列表
function uninstall_components {
    sudo yum -y erase $1 || exit_on_error
    sudo yum -y autoremove
}

# 扩展删除方法
function uninstall_dependencies_ext(){
    echo  "删除 nginx repo"
    sudo rm -f /etc/yum.repos.d/nginx.repo || exit_on_error
    echo  "删除 node repo"
    sudo rm -f /etc/yum.repos.d/nodesource-el7.repo || exit_on_error
    return 0
}

# 定义脚本中用到的程序名对应的软件包
declare -A BASE_TOOL_PKGS=(\
    [grep]=grep \
    [awk]=gawk \
    [nc]=nmap-ncat \
    [curl]=curl \
    [wget]=wget \
    [sed]=sed \
    [dialog]=dialog \
    [zip]=zip \
    [unzip]=unzip \
    [bzip2]=bzip2 \
)

# 判断指定的软件包是否安装,已经安装返回0,否则返回1
# $1 软件包名
function is_installed_pkg(){
    [ -n "$1" ] || return 1
    rpm -qa $1 | grep -F "$1" >/dev/null
}

# 安装简体中文支持
function install_chinese_support(){
    local name=ibus-table-chinese
    is_installed_pkg $name || install_components $name
}
